// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>
#include <pollux/testing/gtest_utils.h>

#include <pollux/functions/lib/check_nested_nulls.h>
#include <pollux/testing/vector/vector_test_base.h>

namespace kumo::pollux::functions::test {
class CheckNestedNullsTest : public testing::Test,
                             public kumo::pollux::VectorBuilder {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }
};

TEST_F(CheckNestedNullsTest, array) {
  auto baseVector = make_array_vector_from_json<int32_t>({
      "null",
      "[6, 7]",
      "[2, null]",
  });
  DecodedVector decodedVector(*baseVector);
  const auto* indices = decodedVector.indices();

  for (bool throwOnNestedNulls : {true, false}) {
    ASSERT_TRUE(
        checkNestedNulls(decodedVector, indices, 0, throwOnNestedNulls));
    ASSERT_FALSE(
        checkNestedNulls(decodedVector, indices, 1, throwOnNestedNulls));
  }

  POLLUX_ASSERT_THROW(
      checkNestedNulls(decodedVector, indices, 2, true),
      "ARRAY comparison not supported for values that contain nulls");
  ASSERT_FALSE(checkNestedNulls(decodedVector, indices, 2, false));
}

TEST_F(CheckNestedNullsTest, map) {
  auto baseVector = make_map_vector_from_json<int32_t, int32_t>({
      "null",
      "{4: 7, 1: 2}",
      "{6: 8, 8: null}",
  });
  DecodedVector decodedVector(*baseVector);
  const auto* indices = decodedVector.indices();

  for (bool throwOnNestedNulls : {true, false}) {
    ASSERT_TRUE(
        checkNestedNulls(decodedVector, indices, 0, throwOnNestedNulls));
    ASSERT_FALSE(
        checkNestedNulls(decodedVector, indices, 1, throwOnNestedNulls));
  }

  POLLUX_ASSERT_THROW(
      checkNestedNulls(decodedVector, indices, 2, true),
      "MAP comparison not supported for values that contain nulls");
  ASSERT_FALSE(checkNestedNulls(decodedVector, indices, 2, false));
}

TEST_F(CheckNestedNullsTest, row) {
  auto baseVector = make_row_vector(
      {
          make_flat_vector<StringView>({
              "a"_sv,
              "b"_sv,
              "c"_sv,
          }),
          make_nullable_flat_vector<StringView>({
              "aa"_sv,
              "bb"_sv,
              std::nullopt,
          }),
      },
      [](vector_size_t row) { return row == 0; });
  DecodedVector decodedVector(*baseVector);
  const auto* indices = decodedVector.indices();

  for (bool throwOnNestedNulls : {true, false}) {
    ASSERT_TRUE(
        checkNestedNulls(decodedVector, indices, 0, throwOnNestedNulls));
    ASSERT_FALSE(
        checkNestedNulls(decodedVector, indices, 1, throwOnNestedNulls));
  }

  POLLUX_ASSERT_THROW(
      checkNestedNulls(decodedVector, indices, 2, true),
      "ROW comparison not supported for values that contain nulls");
  ASSERT_FALSE(checkNestedNulls(decodedVector, indices, 2, false));
}
} // namespace kumo::pollux::functions::test
